package com.xyw.code.efficient_programming.lambda.stream;

import com.alibaba.fastjson.JSONObject;
import com.xyw.code.efficient_programming.lambda.cart.CartService;
import com.xyw.code.efficient_programming.lambda.cart.CategoryEnum;
import com.xyw.code.efficient_programming.lambda.cart.Sku;
import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by xuyiwei on 2020/1/6 14:24
 * 想看看购物车中有什么商品
 * 图书类商品都给买
 * 其余的商品中买两件最贵的
 * 只需要两件总价
 */
public class StreamVs {
    @Test
    public void test1(){
        CartService.cartSkuList.forEach(n-> System.out.println(JSONObject.toJSONString(n)));
    }

    @Test
    public void test2(){
        List<Sku> list = CartService.cartSkuList.stream().filter(n->n.getSkuCategory().equals(CategoryEnum.BOOKS)).collect(Collectors.toList());
        list.forEach(n->System.out.println(JSONObject.toJSONString(n)));
    }

    @Test
    public void test3(){
        List<Sku> list = CartService.cartSkuList.stream().filter(n->!n.getSkuCategory().equals(CategoryEnum.BOOKS))
                .sorted(((o1, o2) -> {
                    if(o1.getSkuPrice()>o2.getSkuPrice()){
                        //-1是往前走
                        return -1;
                    }else{
                        return 1;
                    }
                })).limit(2).collect(Collectors.toList());
//        list.forEach(n->System.out.println(JSONObject.toJSONString(n)));
        list.stream().peek(n-> System.out.println(JSONObject.toJSONString(n)));
    }

    @Test
    public void test4(){
        List<Sku> list = CartService.cartSkuList.stream().filter(n->!n.getSkuCategory().equals(CategoryEnum.BOOKS))
                .sorted(Comparator.comparing(Sku::getSkuPrice).reversed()).limit(2).collect(Collectors.toList());
//        list.forEach(n->System.out.println(JSONObject.toJSONString(n)));
        list.stream().peek(n-> {
            System.out.println(JSONObject.toJSONString(n));
        });
    }

    @Test
    public void test5(){
        CartService.cartSkuList.stream()
                .peek(sku -> System.out.println(JSONObject.toJSONString(sku))).forEach(System.out::println);
//        CartService.cartSkuList.forEach(sku ->System.out.println(JSONObject.toJSONString(sku)) );
//        CartService.cartSkuList.stream().peek(n-> System.out.println(JSONObject.toJSONString(n)));
//        List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8);
//        list.stream()
//                .peek(integer -> System.out.println("accept:" + integer))
//                .forEach(System.out::println);
    }

    @Test
    public void test6() {
         CartService.cartSkuList.stream().flatMap(sku -> Arrays.asList(sku,sku).stream()).collect(Collectors.toList()).forEach(
                n-> System.out.println(JSONObject.toJSONString(n))
        );
    }

    @Test
    public void test7() {
        CartService.cartSkuList.stream()
                //peek在每次集合处理前都会去操作
                .peek(n-> System.out.println(n.getSkuName()))
                .sorted(Comparator.comparing(Sku::getSkuPrice))
                .forEach(n-> System.out.println(n));
    }

    //allMatch anyMatch noneMatch  findFirst  findAny
    //findFirst  findAny串行没区别
    //并行的时候也可能会有区别

    //mapToDouble max 获取最大值
    //mapToDouble min 获取最小值
    //count 获取元素总数
    //
}
